<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Think\Controller;
use Think\page;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class ApiBaseController extends Controller
{

    /* 空操作，用于输出404页面 */
    public function _empty()
    {
        die('No Page');
    }


    /**
     * 通用分页列表数据集获取方法
     *
     *  可以通过url参数传递where条件,例如:  index.html?name=asdfasdfasdfddds
     *  可以通过url空值排序字段和方式,例如: index.html?_field=id&_order=asc
     *  可以通过url参数r指定每页数据条数,例如: index.html?r=5
     *
     * @param array $args 一个多个function条件组成的数组
     *                  array(
     *                  'model'=>'模型名称或者M('')模型',
     *                  'where'=>array('')(数组条件),
     *                  'order'=>'id desc',
     *                  'field'  =>'id,..'(需要查询的列用逗号拼接额),
     *                  'page_param'=>array('sex'=>$sex,..)(分页查询参数，数组传入，对应key对应值),
     *                  'listrows'=>15,(每页显示条数),
     *                  'group' =>'',(分组查询参数),
     *                  'count'  =>'',根据指定字符串计算条数 比如distinct
     *                  'join'  =>array(),联表查询字符串数组
     *                  'alias' =>'' 别名字符串 主要用于用于上面的join
     *                  )
     * 返回数据集
     *
     */
    protected function lists($args,$returnPage = false,$needlimit=true)
    {
        $options = array();
        $model = $args['model'];
        $order = $args['order'];
        $where = $args['where'];
        $listRows = $args['listrows'];
        $param = $args['page_param'];
        $field = $args['field'];
        $group = $args['group'];
        $count = $args['count'];
        $join = $args['join'];
        $alias = $args['alias'];

        if (is_string($model)) {
            $model = M($model);
        }

        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);

        $pk = $model->getPk();

        if (empty($order)) {
            $order=$pk . ' desc';
            if(!empty($alias)){
                $order = $alias.'.'.$pk . ' desc';
            }
            $options['order'] = $order;
        } else {
            $options['order'] = $order;
        }

        if (empty($where)) {
            $where = ' 1=1 ';
        }
        if (!empty($where)) {
            $options['where'] = $where;
        }

        if (!empty($group)) {
            $options['group'] = $group;
        }

        if (empty($count)) {
            $count = '*';
        }
        $options['count'] = $count;

        if (!empty($alias)) {
            $options['alias'] = $alias;
        }


        $options = array_merge((array)$OPT->getValue($model), $options);

        if (!empty($join)) {
            $total = $model->alias($alias)->where($options['where'])->join($join)->field("count($count) as count")->find();
        } else {
            $total = $model->alias($alias)->where($options['where'])->field("count($count) as count")->find();
        }
        // echo $model->getLastSql();exit;
        $total = $total['count'];

        if (empty($listRows)) {
            $listRows = 20;
        }

        if (empty($field)) {
            $field = true;
        }

        $page = new \Think\Page($total, $listRows, $param);
        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');

        $this->assign('_total', $total);
        if($needlimit===true){
            $options['limit'] = $page->firstRow . ',' . $page->listRows;
        }

        $model->setProperty('options', $options);

        if (!empty($join)) {
            $list = $model->alias($alias)->join($join)->field($field)->select();
        } else {
            $list = $model->alias($alias)->field($field)->select();
        }

        if ($returnPage) {
            return array('page' => $page, 'list' => $list);
        } else {
            return $list;
        }

    }

    protected function listsFromSql($args,$returnPage = false,$needlimit=true)
    {
        $options = array();
        $model = $args['model'];
        $order = $args['order'];
        $where = $args['where'];
        $listRows = $args['listrows'];
        $param = $args['page_param'];
        $field = $args['field'];
        $group = $args['group'];
        $count = $args['count'];
        $join = $args['join'];
        $alias = $args['alias'];
        $count_sql_str = $args['count_sql'];
        $select_sql_str = $args['select_sql'];

        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);

        $pk = $model->getPk();

        if (empty($order)) {
            $options['order'] = $pk . ' desc';
        } else {
            $options['order'] = $order;
        }

        if (empty($where)) {
            $where = ' 1=1 ';
        }
        if (!empty($where)) {
            $options['where'] = $where;
        }

        if (!empty($group)) {
            $options['group'] = $group;
        }

        if (empty($count)) {
            $count = '*';
        }
        $options['count'] = $count;

        if (!empty($alias)) {
            $options['alias'] = $alias;
        }


        $options = array_merge((array)$OPT->getValue($model), $options);

        // echo $model->getLastSql();exit;
        //获得总条数的sql
        $total = M()->query($count_sql_str);
        $total = $total[0]['count'];

        if (empty($listRows)) {
            $listRows = 20;
        }

        if (empty($field)) {
            $field = true;
        }

        $page = new \Think\Page($total, $listRows, $param);
        if ($total > $listRows){
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');

        $this->assign('_total', $total);
        if($needlimit===true){
            $options['limit'] = $page->firstRow . ',' . $page->listRows;
        }

        $model->setProperty('options', $options);

        //查询集合的sql
        $list = M()->query($select_sql_str.(empty($options['limit'])?'':' limit '.$options['limit']));

        if ($returnPage){
            return array('page' => $page, 'list' => $list);
        } else {
            return $list;
        }
        exit;

    }

    protected function getListsSql($args,$returnPage = false,$needlimit=true)
    {
        $options = array();
        $model = $args['model'];
        $order = $args['order'];
        $where = $args['where'];
        $listRows = $args['listrows'];
        $param = $args['page_param'];
        $field = $args['field'];
        $group = $args['group'];
        $count = $args['count'];
        $join = $args['join'];
        $alias = $args['alias'];

        if (is_string($model)) {
            $model = M($model);
        }

        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);

        $pk = $model->getPk();

        if (empty($order)) {
            $options['order'] = $pk . ' desc';
        } else {
            $options['order'] = $order;
        }

        if (empty($where)) {
            $where = ' 1=1 ';
        }
        if (!empty($where)) {
            $options['where'] = $where;
        }

        if (!empty($group)) {
            $options['group'] = $group;
        }

        if (empty($count)) {
            $count = '*';
        }
        $options['count'] = $count;

        if (!empty($alias)) {
            $options['alias'] = $alias;
        }


        $options = array_merge((array)$OPT->getValue($model), $options);

        if (!empty($join)) {
            $total = $model->alias($alias)->where($options['where'])->join($join)->field("count($count) as count")->find();
        } else {
            $total = $model->alias($alias)->where($options['where'])->field("count($count) as count")->find();
        }
        // echo $model->getLastSql();exit;
        $total = $total['count'];

        if (empty($listRows)) {
            $listRows = 20;
        }

        if (empty($field)) {
            $field = true;
        }

        $page = new \Think\Page($total, $listRows, $param);
        if($total > $listRows){
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');

        $this->assign('_total', $total);
//        if($needlimit===true){
//            $options['limit'] = $page->firstRow . ',' . $page->listRows;
//        }

        $model->setProperty('options', $options);

        if (!empty($join)) {
            $list_sql = $model->fetchSql(true)->alias($alias)->join($join)->field($field)->select();
        } else {
            $list_sql = $model->fetchSql(true)->alias($alias)->field($field)->select();
        }

       return $list_sql;

    }

	
	

}

